<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible"/>
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
    <link href="./styles/index.css" rel="stylesheet"/>
    <title>Title</title>
</head>
<body>

<div class="score-case" id="app">
    <div class="table">
        <table>
            <thead>
            <tr>
                <th>编号</th>
                <th>科目</th>
                <th>成绩</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody v-if="list.length > 0">
            <tr :key="item.id" v-for="(item,index) in list">
                <td>{{ index + 1 }}</td>
                <td>{{ item.subject }}</td>
                <td :class="{ red:item.score < 60 }">{{ item.score }}</td>
                <td><a @click.prevent="del(item.id)" href="https://baidu.com">删除</a></td>
            </tr>
            </tbody>
            <tbody v-else>
            <tr>
                <td colspan="5">
                    <span class="none">暂无数据</span>
                </td>
            </tr>
            </tbody>

            <tfoot>
            <tr>
                <td colspan="5">
                    <span>总分：{{ totalScore }}</span>
                    <span style="margin-left: 50px">平均分：{{averageScore}}</span>
                </td>
            </tr>
            </tfoot>
        </table>
    </div>
    <div class="form">
        <div class="form-item">
            <div class="label">科目：</div>
            <div class="input">
                <input
                        placeholder="请输入科目"
                        type="text"
                        v-model.trim="subject"
                />
            </div>
        </div>
        <div class="form-item">
            <div class="label">分数：</div>
            <div class="input">
                <input
                        placeholder="请输入分数"
                        type="text"
                        v-model.number="score"
                />
            </div>
        </div>
        <div class="form-item">
            <div class="label"></div>
            <div class="input">
                <button @click="add" class="submit">添加</button>
            </div>
        </div>
    </div>
</div>

<script src="../vue.global.js"></script>

<script>
    const {createApp, ref, computed} = Vue;
    createApp({
        setup() {
            const list = ref([
                {id: 1, subject: '语文', score: 20},
                {id: 7, subject: '数学', score: 99},
                {id: 12, subject: '英语', score: 70},
            ]);
            const subject = ref('');
            const score = ref('');

            const del = (id) => {
                list.value = list.value.filter(item => item.id !== id);
            };

            const add = () => {
                if (!subject.value || !score.value || typeof score.value !== 'number') {
                    return alert('请输入正确的科目和成绩');
                }

                list.value.push({
                    id: +new Date(),
                    subject: subject.value,
                    score: score.value
                });

                subject.value = '';
                score.value = '';
            };

            const totalScore = computed(() => {
                return list.value.reduce((sum, item) => sum + item.score, 0);
            });

            const averageScore = computed(() => {
                if (list.value.length === 0) {
                    return 0;
                }
                return (totalScore.value / list.value.length).toFixed(2);
            });

            return {list, subject, score, del, add, totalScore, averageScore};
        }
    }).mount('#app');
</script>


</body>
</html>